package com.zf.yichat.service.impl;

import com.zf.yichat.dto.MessageCollectionDto;
import com.zf.yichat.mapper.MessageCollectionMapper;
import com.zf.yichat.model.MessageCollection;
import com.zf.yichat.service.MessageCollectionService;
import com.zf.yichat.utils.common.N3d;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MessageCollectionServiceImpl implements MessageCollectionService {

    @Resource
    private MessageCollectionMapper collectionMapper;

    @Override
    public List<MessageCollectionDto> queryCollectionListByUserId(Long userId) {


        ArrayList<MessageCollectionDto> collectionDtoList = new ArrayList<>();

        List<MessageCollection> collectionList = collectionMapper.queryCollectionListByUserId(userId);
        collectionList.stream().forEach(collection -> {
            MessageCollectionDto dto = new MessageCollectionDto();
            BeanUtils.copyProperties(collection, dto);

            Long id = collection.getUserId();
            Long friendUserId = collection.getFriendUserId();
            try {
                String encodeUserId = N3d.encode(id);
                String encodeFriendUserId = N3d.encode(friendUserId);

                dto.setUserId(encodeUserId);
                dto.setUserId(encodeFriendUserId);
            } catch (Exception e) {
                log.error("userId【"+id+"】加密失败");
            }
            collectionDtoList.add(dto);
        });

        return collectionDtoList;
    }

    @Override
    public int addCollection(MessageCollection collection) {

        collection.setCollectionTime(new Date());
        return collectionMapper.insertSelective(collection);
    }

    @Override
    public int deleteCollectionById(Integer id) {
        return collectionMapper.deleteCollectionById(id);
    }
}
